package com.example.imoocmusicdemo.activitys;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.imoocmusicdemo.R;

public class broadCastReceiver extends BaseActivity {

    private int num=0;
    private MyBroadcastReceiverOne one;
    private MyBroadcastReceiverTwo two;
    private MyBroadcastReceiverThree three;
    private TextView tv_left_content,tv_one,tv_two,tv_three;
    private ImageView horn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broad_cast_receiver);
        init();
        registerReceiver();

    }

    private void registerReceiver() {
        one=new MyBroadcastReceiverOne();
        IntentFilter intentFilter1=new IntentFilter();
        intentFilter1.setPriority(1000);
        intentFilter1.addAction("DUCKS");
        registerReceiver(one,intentFilter1);

        two=new MyBroadcastReceiverTwo();
        IntentFilter intentFilter2=new IntentFilter();
        intentFilter2.setPriority(1000);
        intentFilter2.addAction("DUCKS");
        registerReceiver(two,intentFilter2);

        three=new MyBroadcastReceiverThree();
        IntentFilter intentFilter3=new IntentFilter();
        intentFilter3.setPriority(1000);
        intentFilter3.addAction("DUCKS");
        registerReceiver(three,intentFilter3);
    }


    private void init() {
        horn = findViewById(R.id.iv_horn);
        tv_one = findViewById(R.id.tv_one);
        tv_two = findViewById(R.id.tv_two);
        tv_three = findViewById(R.id.tv_three);
        tv_left_content = findViewById(R.id.tv_left_content);
        horn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                tv_left_content.setVisibility(View.VISIBLE);
                horn.setClickable(false);
                Intent intent=new Intent();
                intent.setAction("DUCKS");
                sendOrderedBroadcast(intent,null);

            }

        });
    }

    private class MyBroadcastReceiverOne extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            tv_one.setVisibility(View.VISIBLE);
            num=num+1;
            tv_one.setText(num+"");
            Log.i("BroadcastReceiver", "第一个广播接收者");
            delay();
        }

    }

    private class MyBroadcastReceiverTwo extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            tv_two.setVisibility(View.VISIBLE);
            num=num+1;
            tv_two.setText(num+"");
            Log.i("BroadcastReceiver", "第一个广播接收者");
            delay();
        }

    }

    private class MyBroadcastReceiverThree extends BroadcastReceiver{
        public void onReceive(Context context, Intent intent) {
            tv_three.setVisibility(View.VISIBLE);
            num=num+1;
            tv_three.setText(num+"");
            Log.i("BroadcastReceiver", "第一个广播接收者");
            delay();
        }
    }


    private void delay() {
        try{
            Thread.sleep(500);
        }catch (InterruptedException E){
            E.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(one);
        unregisterReceiver(two);
        unregisterReceiver(three);
    }
}